package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.result;
import com.example.dto.pieData;
import com.example.dto.statistic;
import com.example.pojo.major;
import com.example.pojo.previous;
import com.example.pojo.tracking;
import com.example.service.TrackService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/track")
public class TrackingController {
    @Autowired
    private TrackService trackService;

    //插入用户就业跟踪信息(用户端)
    @PatchMapping ("/insert")
    public result createTrack(@RequestBody tracking track){
        //判断是否为新的就业信息
        tracking t = trackService.findRecordById(track.getStudentID());
        if(t==null){
            //如果为新则插入
            if(track.getStatus()==null){
                return result.error("请选择就业状态");
            }
            if(track.getStatus()!=null && (StringUtils.isEmpty(track.getUnemployReason()) && StringUtils.isEmpty(track.getEmployer()) && StringUtils.isEmpty(track.getPosition()))){
                return result.error("请继续填写内容");
            }
            if(!StringUtils.isEmpty(track.getUnemployReason()) && track.getStatus()==1){
                return result.error("已就业无须填写待业原因");
            }
            if(track.getStatus()==0&&(!StringUtils.isEmpty(track.getEmployer()) || !StringUtils.isEmpty(track.getPosition()))){
                return result.error("未就业无须填写就业单位和职位");
            }
            trackService.insertTrackInfo(track);
            return result.success("插入成功");
        }
        if(t!=null) {
            //如果存在则更新
             if(track.getStatus()==null){
                 return result.error("请选择就业状态");
             }
            if(track.getStatus()!=null && (StringUtils.isEmpty(track.getUnemployReason()) && StringUtils.isEmpty(track.getEmployer()) && StringUtils.isEmpty(track.getPosition()))){
                return result.error("请继续填写内容");
            }
            if(!StringUtils.isEmpty(track.getUnemployReason()) && track.getStatus()==1){
                return result.error("已就业无须填写待业原因");
            }
            if(track.getStatus()==0&&(!StringUtils.isEmpty(track.getEmployer()) || !StringUtils.isEmpty(track.getPosition()))){
                return result.error("未就业无须填写就业单位和职位");
            }

            trackService.updateTrackInfo(track);
            return result.success("更新成功");
        }

        return result.success(track);
    }

    //统计就业人数和未就业人数（管理员端）
    @GetMapping("/countEmploy")
    public result<List> countEmploy(){
        statistic s = trackService.countEmploy();
        List<pieData> data = Arrays.asList(
                new pieData("未就业人数",s.getUnemploy()),
                new pieData("已就业人数",s.getEmploy())
        );
        return result.success(data);
    }

    //返回所有专业的数组数据（管理员端）
    @GetMapping("/allMajorList")
    public result<List> allMajorList(){
       List<major> m = trackService.allMajorList();
       //map对象转为list
       List<String> s = m.stream()
               .map(list -> (String) list.getMajorName()) // 提取majorName
                .collect(Collectors.toList()); // 收集到List中
        return result.success(s);
    }

    //统计各专业就业人数（管理员端）
    @GetMapping("/countInMajor")
    public result<List> countInMajor(){
        List<Integer> integers = trackService.countInMajor();
        return result.success(integers);
    }

    //历年数据对比（管理员端）
    @GetMapping("/previousData")
    public result<List> previousData(){
        List<previous> p = trackService.previousData();
        //map对象转为list
        List<String> years = p.stream()
                .map(list -> (String) list.getYears())
                .collect(Collectors.toList()); // 收集到List中
        //map对象转为list
        List<Integer> number = p.stream()
                .map(list -> (Integer) list.getNumber())
                .collect(Collectors.toList());

       return result.success(Arrays.asList(years,number));
    }

    //删除用户就业信息（管理员端）
    @DeleteMapping("/deleteTrack")
    public result deleteTrack(@RequestParam String id){
        trackService.deleteTrack(id);
        return result.success("删除成功");
    }

    //修改用户就业信息（管理员端）
    @PostMapping("/editTrack")
    public result editTrack(@RequestBody tracking t){
        tracking temp = trackService.findRecordById(t.getStudentID());
//        System.out.println(temp);
        if(temp != null){
            trackService.editTrack(t);
            return result.success("修改成功");
        }
        return result.error("没有这条信息");
    }

    //姓名查找用户就业信息
    @GetMapping("/findByName")
    public result find(@RequestParam String name){
        Page<tracking> trackingPage = new Page<>(1,5);
        IPage<tracking> res = trackService.findByName(trackingPage,name);
        if(res.getTotal() == 0){
            return result.error("暂无此人");
        }
        return  result.success(res);
    }

}
